package com.kingzoon.seckill.service.impl.account;

import java.util.HashMap;
import java.util.List;

import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import ch.qos.logback.classic.Logger;
import payment.tools.util.GUID;

import com.kingzoon.seckill.dao.UaLogLoginMapper;
import com.kingzoon.seckill.dao.UaLoginMethodMapper;
import com.kingzoon.seckill.dto.Result;
import com.kingzoon.seckill.entity.UaLogLogin;
import com.kingzoon.seckill.entity.UaLoginMethod;
import com.kingzoon.seckill.entity.UaLoginMethodExample;
import com.kingzoon.seckill.enums.MacroDefine;
import com.kingzoon.seckill.service.ThirdPartyLoginService;
import com.kingzoon.seckill.service.impl.base.PasswordHelper;
import com.kingzoon.seckill.util.UtilTools;

/**
 * 会员登陆
 * @author raymond
 *
 */
@Service
public class ThirdPartyLoginServiceImpl implements ThirdPartyLoginService {

	private Logger log = (Logger) LoggerFactory.getLogger(ThirdPartyLoginServiceImpl.class);

	private HashMap<String, Object> paramMap = new HashMap<String, Object>();

	@Autowired
	private UaLoginMethodMapper uaLoginMethodDao;

	@Autowired
	private UaLogLoginMapper uaLogLoginDao;

	@Autowired
	private PasswordHelper passwordHelper;

	private UaLoginMethod uaLoginMethod = new UaLoginMethod();
	private UaLogLogin uaLogLogin = new UaLogLogin();
	private UaLoginMethodExample uaLoginMethodExample = new UaLoginMethodExample();

	public Result<HashMap<String, Object>> doBusiness(HashMap<String, Object> mapParam, Result<HashMap<String, Object>> rs) {
		paramMap = mapParam;
		if (preCheck(rs)) {
			if (cache(rs)) {
				if (check(rs)) {
					if (before(rs)) {
						if (clear(rs)) {
							after(rs);
						}
					}
				}
			}
		}
		return rs;
	}

	public boolean preCheck(Result<HashMap<String, Object>> rs) {
		String[] keys = { "exchangeId", "distributorId", "openId", "fromWay", "loginName" };
		String emptyString = UtilTools.isParamEmpty(keys, paramMap);
		if (!UtilTools.isEmpty(emptyString)) {
			rs.setFlag(false);
			rs.setCode(MacroDefine.GS_CODE.E_1002.getCode());
			rs.setMessage(MacroDefine.GS_CODE.E_1002.getMessage());
			return false;
		}
		return true;
	}

	public boolean cache(Result<HashMap<String, Object>> rs) {
		return true;
	}

	public boolean check(Result<HashMap<String, Object>> rs) {
		return true;
	}

	public boolean before(Result<HashMap<String, Object>> rs) {
		String memberId = GUID.generateGUID();
		paramMap.put("memberId", memberId);
		paramMap.put("loginType", UtilTools.isEmpty(UtilTools.valueOf(paramMap.get("loginType"))) ? "0" : paramMap.get("loginType"));

		//登陆信息表
		uaLoginMethod.setExchangeId(UtilTools.valueOf(paramMap.get("exchangeId")));
		uaLoginMethod.setDistributorId(UtilTools.valueOf(paramMap.get("distributorId")));
		uaLoginMethod.setMemberId(UtilTools.valueOf(paramMap.get("memberId")));
		uaLoginMethod.setLoginType(UtilTools.valueOf(paramMap.get("loginType")));
		uaLoginMethod.setLoginName(UtilTools.valueOf(paramMap.get("loginName")));
		uaLoginMethod.setLoginOpenId(UtilTools.valueOf(paramMap.get("openId")));
		uaLoginMethod.setLoginFromWay(UtilTools.valueOf(paramMap.get("fromWay")));
		return true;
	}

	@Transactional
	public boolean clear(Result<HashMap<String, Object>> rs) {
		String onlineSatatus = "1";//0：离线，1：在线

		//检查登陆方法表
		uaLoginMethodExample.createCriteria().andLoginOpenIdEqualTo(UtilTools.valueOf(paramMap.get("openId"))).andLoginFromWayEqualTo(UtilTools.valueOf(paramMap.get("fromWay")));
		List<UaLoginMethod> methodList = uaLoginMethodDao.selectByExample(uaLoginMethodExample);
		if (methodList == null || methodList.size() <= 0) {
			//写入登陆信息表
			int b = uaLoginMethodDao.insertSelective(uaLoginMethod);
			if(b != 1){
				rs.setFlag(false);
				rs.setCode(MacroDefine.JC_CODE.R_9999.getCode());
				rs.setMessage(MacroDefine.JC_CODE.R_9999.getMessage());
				return false;
			}
		}
		uaLoginMethod = methodList.get(0);

		//写入登陆日志表
		uaLogLogin.setExchangeId(UtilTools.valueOf(paramMap.get("exchangeId")));
		uaLogLogin.setDistributorId(UtilTools.valueOf(paramMap.get("distributorId")));
		uaLogLogin.setMemberId(UtilTools.valueOf(paramMap.get("memberId")));
		uaLogLogin.setLoginSerialno(UtilTools.valueOf(paramMap.get("loginSerialNo")));
		uaLogLogin.setLoginName(UtilTools.valueOf(paramMap.get("loginName")));
		uaLogLogin.setLoginType(UtilTools.valueOf(paramMap.get("loginType")));
		uaLogLogin.setLoginDate(Long.valueOf(UtilTools.valueOf(paramMap.get("loginDate"))));
		uaLogLogin.setLoginTime(Long.valueOf(UtilTools.valueOf(paramMap.get("loginTime"))));
		uaLogLogin.setLoginIp(UtilTools.valueOf(paramMap.get("loginIp")));
		uaLogLogin.setLoginFromWay(UtilTools.valueOf(paramMap.get("loginFromWay")));
		uaLogLogin.setLoginResult(onlineSatatus);
		int b = uaLogLoginDao.insertSelective(uaLogLogin);
		if (b != 1) {
			rs.setFlag(false);
			rs.setCode(MacroDefine.JC_CODE.R_9999.getCode());
			rs.setMessage(MacroDefine.JC_CODE.R_9999.getMessage());
			return false;
		}

		return true;
	}

	public boolean after(Result<HashMap<String, Object>> rs) {
		HashMap<String, Object> respMap = new HashMap<String, Object>();
		respMap.put("exchangeId", uaLoginMethod.getExchangeId());
		respMap.put("distributorId", uaLoginMethod.getDistributorId());
		respMap.put("memberId", uaLoginMethod.getMemberId());
		respMap.put("loginName", uaLoginMethod.getLoginName());
		respMap.put("loginOpenId", uaLoginMethod.getLoginOpenId());
		respMap.put("loginFromWay", uaLoginMethod.getLoginFromWay());
		rs.setData(respMap);
		rs.setFlag(true);
		rs.setCode(MacroDefine.JC_CODE.S_0000.getCode());
		rs.setMessage(MacroDefine.JC_CODE.S_0000.getMessage());
		return true;
	}

}
